<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Enums\AdminLetterIsReadEnum;
use App\Exceptions\ApiException;
use App\Models\AdminLetter;
use Illuminate\Support\Facades\DB;

class AdminLetterService
{
    public function getAdminLetters(int $adminUserId, int $page, int $pageSize, array $search)
    {
        $builder = AdminLetter::query()->where('admin_user_id', $adminUserId);
        if (isset($search['is_read'])) {
            $builder->where('is_read', $search['is_read']);
        }
        $count = $builder->count();
        $list = $builder->skip(($page - 1) * $pageSize)->take($pageSize)->get();

        return [
            'list' => $list,
            'count' => $count,
            'page' => $page,
            'page_size' => $pageSize,
        ];
    }

    public function makeReaded(int $id, int $adminUserId)
    {
        /** @var null|AdminLetter $letter */
        $letter = AdminLetter::query()->where('admin_user_id', $adminUserId)
            ->where('id', $id)->first();

        if (empty($letter)) {
            throw new ApiException('数据不存在', 1);
        }

        $letter->is_read = AdminLetterIsReadEnum::READED;
        $letter->save();
        return true;
    }

    public function makeAllReaded(int $adminUserId)
    {
        AdminLetter::query()->where('admin_user_id', $adminUserId)
            ->update([
                'is_read' => AdminLetterIsReadEnum::READED,
            ]);
        return true;
    }

    public function checkHaveLetterNotRead(int $adminUserId)
    {
        $count = AdminLetter::query()
            ->where('admin_user_id', $adminUserId)
            ->where('is_read', AdminLetterIsReadEnum::NOT_READ)
            ->count();

        return [
            'has_red' => $count > 0,
            'number' => $count,
        ];
    }

    public function checkHaveLetterGroupType(int $adminUserId)
    {
        return AdminLetter::query()
            ->where('admin_user_id', $adminUserId)
            ->where('is_read', AdminLetterIsReadEnum::NOT_READ)
            ->select(DB::raw('count(*) as number'), 'type')
            ->groupBy('type')
            ->get()
            ->map(function ($row) {
                return [
                    'number' => $row->number,
                    'type' => $row->type,
                    'has_red' => $row->number > 0,
                ];
            })
            ->toArray();
    }

    public function makeReadedByType(int $adminUserId, int $type)
    {
        AdminLetter::query()->where('admin_user_id', $adminUserId)
            ->where('type', $type)
            ->update([
                'is_read' => AdminLetterIsReadEnum::READED,
            ]);
        return true;
    }

    public function delete(int $adminUserId, int $id)
    {
        AdminLetter::query()->where('admin_user_id', $adminUserId)
            ->where('id', $id)->delete();
        return true;
    }
}
